class SignUpController < ApplicationController
	before_filter :redireciona_index

  def index
    if (params['authenticity_token'].nil?)
      @user = session[:user_id]
    end    
	end

  def new_account
      @user = OpenStruct.new(params)
      @user.language = Language.find(@user.language_id)
      @user.favorite_club = (!@user.club_id.blank?)?Club.find(params['club_id']):nil
      if !valida_preenchimento_form
        flash.now[:warning] = I18n.t(:tr_msg_fields_not_filled) << I18n.t(:tr_msg_fill_fields) << @campos
        render :action => "index"
      else
        @player = Player.create
        @player.authId = session[:user_id].auth_provider #@user.auth_provider
        @player.fullName = @user.fullname
        @player.eMail = @user.email
        @player.nickName = @user.nickname
        @player.language = @user.language
        @player.favorite_club = @user.favorite_club
        @player.save
        session[:player] = @player
        redirect_to :controller => "canelada"
      end
  end

  private

  #todo use of ActiveForm for validations
  def valida_preenchimento_form()
    ret = true
    @campos = "";
    if (@user.fullname.blank?)
      @campos << I18n.t(:tr_full_name)
      ret = false
    end
    if (@user.nickname.blank?)
      @campos << ((@campos.blank?)?"":", ") << I18n.t(:tr_nickname)
      ret = false
    end
    if (@user.email.blank?)
      @campos << ((@campos.blank?)?"":", ") << I18n.t(:tr_email)
      ret = false
    end
    if (@user.language.nil?)
      @campos << ((@campos.blank?)?"":", ") << I18n.t(:tr_language)
      ret = false
    end
    if (@user.favorite_club.nil?)
      @campos << ((@campos.blank?)?"":", ") << I18n.t(:tr_favclub)
      ret = false
    end
    ret
  end

  def redireciona_index
    if session[:user_id].nil?
       redirect_to(:controller => "canelada", :action => "main")
    end
  end

end
